using System;
using System.Xml;
using System.Configuration;
using System.Collections.Specialized;
using System.Collections.Generic;

namespace Subgurim.Controles.SSM
{
    /// <summary>
    /// Summary description for SiteMapSectionHandler
    /// </summary>
    public class SubgurimSiteMapSectionHandler : IConfigurationSectionHandler
    {
        public SubgurimSiteMapSectionHandler()
        {
            //
            // TODO: Add constructor logic here
            //
        }

        public object Create(object parent, object configContext, XmlNode section)
        {
            SubgurimSiteMapConfiguration config = new SubgurimSiteMapConfiguration();

            // DATAPATH
            if ((section.SelectSingleNode("dataPath") == null)
                || (string.IsNullOrEmpty(section.SelectSingleNode("dataPath").InnerText)))
                config.dataPath = "~/App_Data/SiteMap/";
            else
                config.dataPath = section.SelectSingleNode("dataPath").InnerText;

            // SITEMAPRESPONSE
            if ((section.SelectSingleNode("sitemapResponse") == null)
                || (string.IsNullOrEmpty(section.SelectSingleNode("sitemapResponse").InnerText)))
                config.sitemapResponse = "sitemap.aspx";
            else
                config.sitemapResponse = section.SelectSingleNode("sitemapResponse").InnerText;

            
            // FORBIDDENPATHS
            XmlNode forbiddenPaths_node = section.SelectSingleNode("forbiddenPaths");
            if ((forbiddenPaths_node == null) || (!forbiddenPaths_node.HasChildNodes))
                config.forbiddenPaths = new StringCollection();
            else
            {
                foreach (XmlNode subnode in forbiddenPaths_node.ChildNodes)
                {
                    config.forbiddenPaths.Add(SubgurimSiteMapUtilities.getCleanNodeValue(subnode.InnerText));
                }
            }

            
            // ACCEPTEDEXTENSIONS
            XmlNode acceptedExtensions_node = section.SelectSingleNode("acceptedExtensions");
            if ((acceptedExtensions_node == null) || (!acceptedExtensions_node.HasChildNodes))
                config.acceptedExtensions = new StringCollection();
            else
            {
                foreach (XmlNode subnode in acceptedExtensions_node.ChildNodes)
                {
                    config.acceptedExtensions.Add(SubgurimSiteMapUtilities.getCleanNodeValue(subnode.InnerText));
                }
            }


            // REWRITERRULES
            XmlNode rewriterRules_node = section.SelectSingleNode("rewriterRules");
            if ((rewriterRules_node == null) || (!rewriterRules_node.HasChildNodes))
                config.rewriterRules = new List<RewriterRule>();
            else
            {
                foreach (XmlNode subnode in rewriterRules_node.ChildNodes)
                {
                    if (subnode.HasChildNodes)
                    {
                        string oldPath = SubgurimSiteMapUtilities.getCleanNodeValue(subnode.SelectSingleNode("oldPath").InnerText);
                        string newPath = SubgurimSiteMapUtilities.getCleanNodeValue(subnode.SelectSingleNode("newPath").InnerText);
                        if (!string.IsNullOrEmpty(oldPath) && !string.IsNullOrEmpty(newPath))
                        {
                            config.rewriterRules.Add(new RewriterRule(oldPath, newPath));
                        }
                    }
                }
            }
            
            return config;
        }
    }
}